﻿using System.Collections.Generic;
using GMap.NET.MapProviders;

namespace GMap.NET.EXT
{
    public enum FetchType { None, World, China, View };

    public class ZoomFetcher
    {
        public const int ZoomMax = 20;
        public const int ZoomMin = 1;

        private int zoom;

        private RectLatLng area = RectLatLng.Empty;
        public RectLatLng Area { get => area; set => area = value; }

        private long tileCount;
        public long TileCount { get => tileCount; }

        private long byteCount;
        public long ByteCount { get => byteCount; }
        public string ByteString { get => getByteString(byteCount); }
        

        public ZoomFetcher(int zoom)
        {
            this.zoom = zoom;
        }

        public void calculate(GMapProvider provider)
        {
            if(!area.IsEmpty)
            {
                List<GPoint> gps = provider.Projection.GetAreaTileList(area, zoom, 0);
                tileCount += gps.Count;
                byteCount += gps.Count * 20;
            }
        }

        public void fetch(GMapProvider provider)
        {
            if (!area.IsEmpty)
            {
                TilePrefetcher prefetcher = new TilePrefetcher();
                prefetcher.Start(area, zoom, provider, 100, 1000);
            }
        }

        public static string getByteString(long fileKB)
        {
            string SizeText = string.Empty;

            if (fileKB < 1000)
            {
                SizeText = string.Format("{0} KB", fileKB.ToString());
            }
            else
            {
                long title = fileKB / 1000;
                if (title < 1000)
                {
                    long tail = fileKB % 1000;

                    if (tail > 0)
                    {
                        tail = tail * 10 / 1000;
                    }

                    if (tail > 0)
                    {
                        SizeText = string.Format("{0}.{1} MB", title.ToString(), tail.ToString());
                    }
                    else
                    {
                        SizeText = string.Format("{0} MB", title.ToString());
                    }
                }
                else
                {
                    long titleM = title / 1000;
                    long tail = title % 1000;

                    if (tail > 0)
                    {
                        tail = tail * 10 / 1000;
                    }

                    if (tail > 0)
                    {
                        SizeText = string.Format("{0}.{1} G", titleM.ToString(), tail.ToString());
                    }
                    else
                    {
                        SizeText = string.Format("{0} G", titleM.ToString());
                    }
                }
            }

            return SizeText;
        }
    }

    public class ZoomFetchers
    {
        private Dictionary<int, ZoomFetcher> dictionary = new Dictionary<int, ZoomFetcher>();

        public ZoomFetchers()
        {
            for (int i = ZoomFetcher.ZoomMin; i <= ZoomFetcher.ZoomMax; i++)
            {
                dictionary[i] = new ZoomFetcher(i);
            }
        }

        public ZoomFetcher this[int key]
        {
            get
            {
                return dictionary[key];
            }
        }

        public void calculate(GMapProvider provider)
        {
            foreach(ZoomFetcher f in dictionary.Values)
            {
                f.calculate(provider);
            }
        }

        public void fetch(GMapProvider provider)
        {
            foreach (ZoomFetcher f in dictionary.Values)
            {
                f.fetch(provider);
            }
        }

    }

}
